import Model from "js-model"
import helper from "../../common/helper.js"
const app = getApp()
console.log(app.globalData.BACK_URL)
let mol = [{
	act_name: String,
	amount: {
		type: Number,
		default: 0
	},
	comp_ids: Number,
	coupon_id: Number,
	coupon_type: String,
	enddate: {
		type: Date,
		format: 'YYYY-MM-DD'
	},
	explain: String,
	id: {
		type: Number,
		default: 0
	},
	image_url: String,
	plantype: String,
	price: {
		type: Number,
		default: 0
	},
	rebate: {
		type: Number,
		default: 0
	},
	startdate: {
		type: Date,
		format: 'YYYY-MM-DD'
	}
}]
class mycoupon extends Model {
	parse(data) {
		let coupon = super.parse(data)
		function format(obj){
			if (helper.isArray(obj)) {
				obj.forEach(v => {
					if (!helper.isEmpty(v.image_url)) v.image_url = app.globalData.BACK_URL + v.image_url
					v.price = parseFloat(v.price)
					v.amount = parseFloat(v.amount)
					v.rebate = parseFloat(v.rebate)
				})
			}
		}
		format(coupon.expired)
		format(coupon.used)
		format(coupon.valid)
		return coupon
	}
	dispose(data, param) {
		return super.dispose(data, param)
	}
}

export default new mycoupon({
	expired: mol,
	used: mol,
	valid: mol
})
